package cn.jtfadmin.cms.service.impl;

import cn.jtfadmin.base.data.jpa.service.JpaTreeService;
import cn.jtfadmin.base.lang.common.domain.dto.*;
import cn.jtfadmin.cms.domain.dto.CmsAddPDTO;
import cn.jtfadmin.cms.domain.dto.CmsEditPDTO;
import cn.jtfadmin.cms.domain.dto.CmsMenuAddPDTO;
import cn.jtfadmin.cms.domain.dto.CmsMenuEditPDTO;
import cn.jtfadmin.cms.domain.entity.CmsMenuEntity;
import cn.jtfadmin.cms.repository.CmsMenuRepository;
import cn.jtfadmin.cms.service.CmsMenuService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

/**
 * cms 菜单信息
 */
@Service
public class CmsMenuServiceImpl extends JpaTreeService<CmsMenuEntity> implements CmsMenuService {

    @Autowired
    protected CmsMenuRepository repository;


    @Override
    protected JpaRepositoryImplementation<CmsMenuEntity, String> getJpaRepository() {
        return repository;
    }

    @Override
    protected Class<?> getAddClass() {
        return CmsMenuAddPDTO.class;
    }

    @Override
    protected Class<? extends EditPDTO> getEditClass() {
        return CmsMenuEditPDTO.class;
    }

    @Override
    protected void beforeSave(CmsMenuEntity cmsMenuEntity) {
        String contentHtml = cmsMenuEntity.getContentHtml();
        if(StringUtils.hasText(contentHtml)){
            Document document = Jsoup.parse(contentHtml);
            cmsMenuEntity.setContentText(document.text());
        }else {
            cmsMenuEntity.setContentText("");
        }

    }
}
